﻿#include "Setting.h"
#include "Creature.h"

using namespace cocos2d;

static float s_factorHP[NUM_OF_OBJ_TYPE] = {
	1.0f,
};

Setting::Setting(void)
{
	d_resString = new ResString();

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	this->_fieldSize = CCSize(winSize.width, 680);
	d_save = CCUserDefault::sharedUserDefault();
	d_maxWave = d_save->getIntegerForKey("wave", 1);
	d_wave = d_maxWave;


	this->d_totalMoney = 0;

	_FactorEnemyMaxHP = 1;
	_FactorEnemyBulletSpeed = _fieldSize.width / 5;
	_FactorEnemyShootInterval = 1;
	_FactorEnemySpeed = _fieldSize.width / 10;
	_FactorEnemyDamage = 1;
	_FactorEnemyMoney = 1;

	_FactorPlayerDamage = 1;
	_FactorTeethHealth = 1;
	_FactorPlayerDeadlyShoot = 1;

	d_gunLevel = 0;
	d_healthLevel = 0;
	d_deadlyShootLevel = 0;

	d_effectVolume = 50;
	d_bgVolume = 50;
}


Setting::~Setting(void)
{
}

int Setting::getHP(int hp, Creature* creature)
{
	if(creature->getObjType() == OBJ_TYPE_ENEMY) {
		return hp * (10 + d_wave) / 10 * _FactorEnemyMaxHP;
	}
	return hp;
}

float Setting::getShootInterval(float shootInterval, Creature* creature)
{
	return shootInterval * _FactorEnemyShootInterval / 100;
}

float Setting::getBulletSpeed(float bulletSpeed, Creature* creature)
{
	return bulletSpeed * _FactorEnemyBulletSpeed / 100;
}

float Setting::getSpeed(float speed, Creature* creature)
{
	return speed * _FactorEnemySpeed / 100;
}

int Setting::getDamage(int damage, Creature* creature)
{
	return damage * (10 + d_wave) / 10 * _FactorEnemyDamage;
}

int Setting::getPlayerDamage()
{
	return d_gunLevel * GUN_UPGRADE + BASE_GUN_POWER;
}

float Setting::getDeadlyShootRate()
{
	return d_deadlyShootLevel * DEADLY_SHOOT_UPGRADE/100;
}

float Setting::getDeadlyShootPower()
{
	return DEADLY_SHOOT_POWER;
}

float Setting::getHealthBuff()
{
	return d_healthLevel * HEALTH_UPGRADE/100 + 1;
}

int Setting::getMoney(int money, Creature* creature)
{
	return money * (10 + d_wave) / 10 * _FactorEnemyMoney;
}

void Setting::earn(int money)
{
	d_totalMoney += money;
}
bool Setting::cost(int money)
{
	if (d_totalMoney > money) {
		d_totalMoney -= money;
		return true;
	} else {
		return false;
	}
}

bool Setting::upgradeGun()
{
	if (d_gunLevel < MAX_GUN_LEVEL && cost(GUN_COST * (d_gunLevel+1))) {
		d_gunLevel++;
		return true;
	}
	return false;
}

bool Setting::upgradeHealth()
{
	if (d_healthLevel < MAX_HEALTH_LEVEL && cost(HEALTH_COST * (d_healthLevel+1))) {
		d_healthLevel++;
		return true;
	}
	return false;
}

bool Setting::upgradeDeadlyShoot()
{
	if (d_deadlyShootLevel < MAX_DEADLY_SHOOT_LEVEL && cost(DEADLY_SHOOT_COST * (d_deadlyShootLevel+1))) {
		d_deadlyShootLevel++;
		return true;
	}
	return false;
}

int Setting::getMaxWave()
{
	return d_maxWave;
}

void Setting::setMaxWave(int wave)
{
	d_maxWave = wave;
	d_save->setIntegerForKey("wave", d_maxWave);
	d_save->flush();
}

int Setting::getWave()
{
	return d_wave;
}

void Setting::setWave(int wave)
{
	d_wave = wave;
	if(d_wave > d_maxWave) {
		setMaxWave(d_wave);
	}
}